﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Web.Management;
using System.Configuration;
using System.Resources;
using System.Data.Linq;
using System.Data.SqlClient;

namespace SurveySystem.Core
{
    public class CommonUtils
    {
        private static string ConnectionString
        {
            get
            {
                string connectionString = ConfigurationManager.ConnectionStrings["SurveySystemConnectionString"].ConnectionString;
                string dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory") as String;
                if (!dataDirectory.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()))
                    dataDirectory += System.IO.Path.DirectorySeparatorChar;
                connectionString = connectionString.Replace("|DataDirectory|", dataDirectory);
                return connectionString;
            }
        }

        public static string OzekiConnectionString
        {
            get
            {
                string connectionString = ConfigurationManager.ConnectionStrings["OzekiConnectionString"].ConnectionString;
                
                return connectionString;
            }
        }


        public static string ApplicationEmailAddress
        {
            get
            {
                return ConfigurationManager.AppSettings["ApplicationEmailAddress"];
            }
        }

        private static ResourceManager _resourceManager = null;
        internal static ResourceManager MessageResources
        {
            get
            {
                if (_resourceManager == null)
                    throw new Exception("Set a MessageResources first!");
                return _resourceManager;
            }
            set { _resourceManager = value; }

        }

        public static SurveySystemDataContext GetDc()
        {
            return new SurveySystemDataContext(ConnectionString);
        }

        public static SqlConnection GetConnection()
        {
            return new SqlConnection(OzekiConnectionString);
        }

        internal static void CreateDb(SurveySystemDataContext dc)
        {
            try
            {
                Debug.Print("Creating database ...");
                dc.CreateDatabase();
                SqlServices.Install(dc.Connection.Database, SqlFeatures.Membership, ConnectionString);
                //SqlServices.Install(dc.Connection.Database, SqlFeatures.All, ConnectionString);               
                Debug.Print("Ok.");
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
                Debug.Print(ex.StackTrace);
                throw ex;
            }
        }

        internal static void ResetDB()
        {
            Debug.Print("ResetDb...");
            SurveySystemDataContext dc = GetDc();
            if (dc.DatabaseExists())
            {
                dc.DeleteDatabase();
            }
            CreateDb(dc);
        }

        internal static void SubmitAndHandleConflicts(SurveySystemDataContext dc)
        {
            try
            {
                dc.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (ChangeConflictException)
            {
                dc.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                dc.SubmitChanges();
            }
        }
    }
}
